import http from '../../utils/request'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show: false,
        expressTemplate: [{name:'请选择'},{name:'教育教学培训'},{name:'原创试卷教辅'},{name:'百师云资源'}],
        cname:'',
        subject:'',
        ctype:'',
        username:'',
        phonenumber:'',
        index:0,
        index1:0,
        area:[],
        items: [{ name:'学校'},{name:'机构'},{name:'其他'}],
        current:-1,
        yewuIshow:false,
        areaIshow:false,
        classifyIshow:false,
        nameIshow:false,
        telIshow:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getArea()

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    async getArea(){
        let that = this
        let options={
                url:'/getSubject',
                method:'POST',
                header:{"content-type":"application/json;charset=UTF-8"}
            }
        let res = await http(options)
        if(res.data.code==0){
            res.data.obj.unshift({subjectname:'请选择'})
            that.setData({area:res.data.obj})
        }
    },
    expressTemplateChange(e){
        let that = this
        that.setData({index:e.detail.value})
        if([e.detail.value]!=0){
            that.setData({cname:that.data.expressTemplate[e.detail.value].name})
        }
    },
    expressTemplateChange1(e){
        let that = this
        that.setData({index1:e.detail.value})
        if([e.detail.value]!=0){
            that.setData({subject:that.data.area[e.detail.value].subjectname})
        }
    },
    radioChange(e){
        let that = this
        this.setData({ctype:that.data.items[e.detail.value].name,current:e.detail.value})
    },
    bindKeyInput(e){
        this.setData({username:e.detail.value})
    },
    bindKeyInput1(e){
        this.setData({phonenumber:e.detail.value})
    },
    async submitSet(){
        let that = this
        if(that.data.cname==''){
            that.setData({yewuIshow:true})
            return false
        }
        if(that.data.subject==''){
            that.setData({areaIshow:true})
            return false
        }
        if(that.data.ctype==''){
            that.setData({classifyIshow:true})
            return false
        }
        if(that.data.username==''){
            that.setData({nameIshow:true})
            return false
        }
        if(that.data.phonenumber==''){
            that.setData({telIshow:true})
            return false
        }
        if(!/^1[3|4|5|8]\d{9}$/.test(that.data.phonenumber)){
            that.setData({telIshow:true})
            return false
        }
        let options={
                url:'/setCooperation',
                method:'POST',
                data:{
                    cname:that.data.cname,
                    subject:that.data.subject,
                    ctype:that.data.ctype,
                    username:that.data.username,
                    phonenumber:that.data.phonenumber
                },
                header:{"content-type":"application/json;charset=UTF-8"}
            }
        let res = await http(options)
        if(res.data.code==0){
            that.setData({yewuIshow:false,areaIshow:false,classifyIshow:false,nameIshow:false,telIshow:false,cname:'',subject:'',ctype:'',username:'',phonenumber:'',index:0,index1:0,current:0})
            wx.showToast({
                title: '申请成功',
                icon: 'success',
                duration: 2000
              })
        }
    }
})